package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.annotation.Bean;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.addProviderBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.addProviderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.providerServiceType;
import com.ymttest.database.model.providerSupportCountry;
import com.ymttest.database.model.serviceProvider;
import com.ymttest.database.sqlwapper.godmch.providerServiceTypeWapper;
import com.ymttest.database.sqlwapper.godmch.providerSupportCountryWapper;
import com.ymttest.database.sqlwapper.godmch.serviceProviderWapper;
/**
*添加服务商
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_addProvider{
	private static addProviderBean bean;
	private static addProviderCall call;
	private serviceProviderWapper serviceWapper=new serviceProviderWapper();
	private providerSupportCountryWapper serviceCountryWapper=new providerSupportCountryWapper();
	private providerServiceTypeWapper serviceTypeWapper=new providerServiceTypeWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加服务商");
	}

	@Before
	public void caseUp() {
		bean=new addProviderBean();
		call=new addProviderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 校验
	 * @param userId_channl
	 * @throws JSONException
	 * @throws InterruptedException
	 */
	private void check_verify(int userId_channl) throws JSONException, InterruptedException
	{
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证msg");
		Logger.verifyEquals(true, call.getSuccess(), "验证success");
		//验证数据库
		Logger.comment("##### 1:验证serviceProvider表 ######");
		serviceProvider service=serviceWapper.selectByPrimaryKey(userId_channl);
		Logger.verifyEquals(0, service.getState(), "验证数据库serviceProvider 中的state字段");
		Logger.verifyEquals(true, service.getCreateTime().toString().length()>10, "验证数据库serviceProvider 中的CreateTime字段");
		Logger.verifyEquals(true, service.getUpdateTime().toString().length()>10, "验证数据库serviceProvider 中的UpdateTime字段");
		
		Logger.comment("##### 2:验证providerSupportCountry表 ######");
		List<providerSupportCountry>  provicerCountryList=serviceCountryWapper.selectByProviderId(userId_channl);
		Logger.verifyEquals(bean.getCountryList().size(), provicerCountryList.size(), "验证数据库providerSupportCountry 中的条数等于参数传入的总数");
		List<String> countrys=provicerCountryList.stream().map(p -> p.getSupportCountry()).collect(Collectors.toList());
		
		provicerCountryList.stream().forEach(a->{
			Logger.verifyEquals(true, countrys.contains(a.getSupportCountry()), "验证数据库providerSupportCountry包含传入的country");
		});
		Logger.comment("##### 3:验证providerServiceType表 ######");
		List<providerServiceType> serviceTypes=serviceTypeWapper.selectByPrimaryKey(userId_channl,null);
		
		Logger.verifyEquals(bean.getServiceType().size(), serviceTypes.size(), "验证传入的ServiceType与数据库返回的条数一致");
		for(int i=0;i<serviceTypes.size();i++)
		{
			providerServiceType serviceType =serviceTypes.get(i);
			Logger.verifyEquals(0, serviceType.getState(), "验证数据库providerServiceType 中的state字段");
		    Logger.verifyEquals(true,bean.getServiceType().contains(serviceType.getServiceType()), "验证数据库providerServiceType 中的ServiceType字段");
		    Logger.verifyEquals(true,bean.getServiceType().contains(serviceType.getProperty()), "验证数据库providerServiceType 中的property字段");
			
			Logger.verifyEquals(true, serviceType.getCreateTime().toString().length()>10, "验证数据库providerServiceType 中的CreateTime字段");
			Logger.verifyEquals(true, serviceType.getUpdateTime().toString().length()>10, "验证数据库providerServiceType 中的UpdateTime字段");
		}

	 }
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_001() {
		Logger.start(true,"正例-添加服务商-服务商是渠道");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			
			check_verify(userId_channl);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_001_3() {
		Logger.start(true,"正例-添加服务商-服务商是纯代理");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			//createmchbean.setCountry("CAN");
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");//设为国内，变为纯代理商
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			//创建服务商
			List<String> countryList=new ArrayList<>();
			countryList.add(createmchbean.getCountry());
			
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			bean.setProviderId(userId_agent);
			bean.setServiceType(serviceType);
			bean.setCountryList(countryList);
			call.setData(bean);
		    call.callService();	
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("服务商必须是渠道商身份"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_001_1() {
		Logger.start(true,"正例-添加服务商-serviceType为空");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			
			bean.setProviderId(userId_channl);
			//bean.setServiceType(1);
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_001_2() {
		Logger.start(true,"正例-添加服务商-serviceType不存在");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(100);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_002() {
		Logger.start(true,"反例-添加服务商-服务商不存在");
		try {
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			bean.setProviderId(999999);
			bean.setServiceType(serviceType);
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("商户不存在"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_003() {
		Logger.start(true,"反例-添加服务商-服务国家列表countryList为空");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN1");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("国家不在系统支持范围内"), "验证msg");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_004() {
		//新增 20191015
		Logger.start(true,"正例-添加服务商-服务商是渠道-服务内容-1：通用版");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setDrawBackMode(1);//服务内容 1：通用版 2：益钱退税
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			
			check_verify(userId_channl);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_005() {
		//新增 20191015
		Logger.start(true,"正例-添加服务商-服务商是渠道-服务内容-2：益钱退税");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setDrawBackMode(2);//服务内容 1：通用版 2：益钱退税
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			
			check_verify(userId_channl);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_006() {
		//新增 20191015
		Logger.start(true,"正例-添加服务商-服务商是渠道-服务内容-1：通用版和 2：益钱退税-返回互斥失败");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			serviceType.add(2);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setDrawBackMode(1);//服务内容 1：通用版 2：益钱退税 3:软件服务商
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("通用版、益钱退税互斥"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_007() {
		//新增 20191015
		Logger.start(true,"正例-添加服务商-服务商是渠道-服务内容-1：通用版和 3：软件服务商");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(1);
			serviceType.add(3);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setDrawBackMode(1);//服务内容 1：通用版 2：益钱退税 3:软件服务商
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			

			check_verify(userId_channl);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProvider_008() {
		//新增 20191015
		Logger.start(true,"正例-添加服务商-服务商是渠道-服务内容-2：益钱退税和 3：软件服务商");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			
			List<String> countryList=new ArrayList<>();
			countryList.add("CAN");
			List<Integer> serviceType=new ArrayList<>();
			serviceType.add(2);
			serviceType.add(3);
			
			bean.setProviderId(userId_channl);
			bean.setServiceType(serviceType);
			bean.setDrawBackMode(1);//服务内容 1：通用版 2：益钱退税 3:软件服务商
			bean.setCountryList(countryList);
			
			call.setData(bean);
			call.callService();			

			check_verify(userId_channl);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}